package com.lcx.movie.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.UserRating;
import com.lcx.movie.service.MovieService;
import com.lcx.movie.service.UserRatingService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李丽君
 * @version 1.0
 * @Description 用户评分控制层
 * @date 2025/2/28 14:01
 */
@RestController
@CrossOrigin(origins = "*")
public class UserRatingController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private UserRatingService userRatingService;
    /**
     * 用户评分
     * @param userRating 评分实体
     * @return 受影响的行数
     */
    @PostMapping("/insertUserRating")
    public String insertUserRating(@RequestBody UserRating userRating){
        int insertUserRating = userRatingService.insertUserRating(userRating);
        if(insertUserRating>0){
            int updateMovieScore = movieService.updateMovieScore();// 评分成功就调用修改电影评分的方法
            System.out.println("修改电影评分:"+updateMovieScore);
            return "success";
        }else {
            return "fail";
        }
    }
    /**
     * 查询用户评分
     * @param userName 用户名
     * @param movieCnName 电影名
     * @return 用户评分集合
     */
    @GetMapping("/selectUserRating")
    public Rep selectUserRating(@Param("page") Integer page,@Param("pageSize") Integer pageSize
            ,@Param("userName") String userName,@Param("movieCnName") String movieCnName){
        // 开启分页
        PageHelper.startPage(page,pageSize);
        // 执行查询
        List<UserRating> userRatings = this.userRatingService.selectUserRating(userName, movieCnName);
        // 创建分页对象
        PageInfo<UserRating> userRatingPageInfo=new PageInfo<>(userRatings);
        // 返回
        return new Rep().addData("userRatingPageInfo",userRatingPageInfo);
    }
}
